﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;


namespace PloobsEngine.SceneControl
{
    /// <summary>
    /// Bloom Post Effect
    /// </summary>
    public class BloomPostEffect : IPostEffect
    {
        #region IPostEffect Members

        SpriteBatch spriteBatch;
        Effect Saturate = null;
        Effect Combine;
        RenderTarget2D renderTarget0;
        RenderTarget2D renderTarget1;
        GaussianBlurPostEffect gaussian;
        EngineStuff engine;
        float bloomThreshold = 0.4f;

        /// <summary>
        /// Bloom Parameter
        /// </summary>
        public float BloomThreshold
        {
            get { return bloomThreshold; }
            set
            {
                bloomThreshold = value;
                if (Saturate != null)
                {
                    Saturate.Parameters["BloomThreshold"].SetValue(bloomThreshold);
                }
            }
        }

        public void Draw(IRenderHelper rHelper, Microsoft.Xna.Framework.GameTime gt, Microsoft.Xna.Framework.Rectangle screenRec, IWorld world)
        {            

            rHelper.SetUpRenderTarget(renderTarget1);
            Saturate.Parameters["current"].SetValue(rHelper[PrincipalConstants.CurrentImage]);
            Saturate.Parameters["halfPixel"].SetValue(EngineStuff.HalfPixel);
            rHelper.RenderTextureToFullScreenVertexPixel(Saturate);
            Texture2D t = rHelper.GetResultsRenderTarget();

            rHelper.SetUpRenderTarget(renderTarget0);
            gaussian.Draw(t,rHelper,  gt, screenRec,world);
            Texture2D x = rHelper.GetResultsRenderTarget();                        
                        
            Combine.Parameters["halfPixel"].SetValue(EngineStuff.HalfPixel);
            Combine.Parameters["base"].SetValue(rHelper[PrincipalConstants.CurrentImage]);
            Combine.Parameters["last"].SetValue(x);
            rHelper.RenderTextureToFullScreenVertexPixel(Combine);    

        }       
              

        public void init(EngineStuff engine)
        {
            this.engine = engine;
            ///Get the shared SpriteBatch
            spriteBatch = EngineStuff.SpriteBatch;
            ///Load the asset
            Saturate = EngineStuff.InternalContentManager.GetAsset<Effect>("Saturate");
            Saturate.Parameters["BloomThreshold"].SetValue(bloomThreshold);
            Combine = EngineStuff.InternalContentManager.GetAsset<Effect>("Combine");
            //Combine = EngineStuff.CustomContentManager.GetAsset<Effect>("..\\Content\\Effects\\Combine");
            ///Get the default Color Buffer from the engine
            renderTarget0 = EngineStuff.GetDefaultColorBuffer();
            renderTarget1 = EngineStuff.GetDefaultColorBuffer();
            ///Create a auxiliar post effect used in this effect
            gaussian = new GaussianBlurPostEffect();
            ///When we use a post effect "by hand" we to initialize them
            (gaussian as IPostEffect).init(engine); 

            
        }  

        #endregion
    }
}
